/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.domain.vo.ClazzandteacherQueryCriteria;
import me.zhengjie.modules.system.mapper.*;
import me.zhengjie.modules.system.service.ClazzandteacherService;
import me.zhengjie.modules.system.service.CourseService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author shitou
* @date 2025-02-03
**/
@Service
@RequiredArgsConstructor
public class ClazzandteacherServiceImpl extends ServiceImpl<ClazzandteacherMapper, Clazzandteacher> implements ClazzandteacherService {

    private final ClazzandteacherMapper clazzandteacherMapper;

    private final DepartmentteacherMapper departmentteacherMapper;

    private final TeacherMapper teacherMapper;

    private final UserMapper userMapper;

    private final CourseteacherMapper courseteacherMapper;

    private final CourseMapper courseMapper;

    @Override
    public PageResult<Clazzandteacher> queryAll(ClazzandteacherQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(clazzandteacherMapper.findAll(criteria, page));
    }

    @Override
    public List<Teacher> queryAll(ClazzandteacherQueryCriteria criteria){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazzId",criteria.getClazzId());
        List<Clazzandteacher> clazzandteacherList = clazzandteacherMapper.selectList(queryWrapper);

        List<Integer> teacherIds = new ArrayList<>();
        for(Clazzandteacher clazzandteacher:clazzandteacherList){
            teacherIds.add(clazzandteacher.getTeacherId());
        }

        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id",teacherIds);
        if(criteria.getName()!=null){
            queryWrapper1.in("username",criteria.getName());
        }
        List<Teacher> teacherList = new ArrayList<>();
        List<Courseteacher> courseteacherList = new ArrayList<>();

        if(teacherIds.size()!=0){
            teacherList = teacherMapper.selectList(queryWrapper1);

            for(Clazzandteacher clazzandteacher:clazzandteacherList){
                for(Teacher teacher:teacherList){
                    if(clazzandteacher.getTeacherId() == Math.toIntExact(teacher.getId())){
                        teacher.setClazzandteacherId(clazzandteacher.getId());
                    }
                }
            }
            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.in("teacherId",teacherIds);
            courseteacherList = courseteacherMapper.selectList(queryWrapper2);
            List<Course> courseList = new ArrayList<>();
            List<Integer> courseIds = new ArrayList<>();
            for(Courseteacher courseteacher:courseteacherList){
                courseIds.add(courseteacher.getCourseId());
            }
            if(courseIds.size()!=0){
                QueryWrapper queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.in("id",courseIds);
                courseList = courseMapper.selectList(queryWrapper3);

                for(Teacher teacher:teacherList){
                    for(Courseteacher courseteacher:courseteacherList){
                        if(Math.toIntExact(teacher.getId()) == courseteacher.getTeacherId()){
                            List<Course> courseList1 = new ArrayList<>();
                            for(Course course:courseList){
                                if(course.getId() == courseteacher.getCourseId()){
                                    courseList1.add(course);
                                }
                            }
                            teacher.setCourseList(courseList1);
                        }
                    }
                }
            }
        }
        return teacherList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Clazzandteacher resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Clazzandteacher resources) {
        Clazzandteacher clazzandteacher = getById(resources.getId());
        clazzandteacher.copy(resources);
        saveOrUpdate(clazzandteacher);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Clazzandteacher> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Clazzandteacher clazzandteacher : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" teacherid",  clazzandteacher.getTeacherId());
            map.put(" userid",  clazzandteacher.getUserId());
            map.put(" clazzid",  clazzandteacher.getClazzId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}